/**
 * 文章评论管理初始化
 */
var Comment = {
    id: "CommentTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Comment.initColumn = function () {
    return [
        {field: 'selectItem',   checkbox: true,  visible: true },
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {
            field: 'avatarOssUrl',
            title: '作者',
            editable: false,
            width: '200px',
            formatter: function (code, row, index) {
                return '<ul class="list-unstyled" style="max-width: 200px;">' +
                    '<li><a href="' + row.url + '" target="_blank"><img src="' + filterXSS(row.avatarOssUrl) + '" style="width: 20px;border-radius: 50%;position: relative;top: -2px;"/>' + filterXSS(row.nickname) + '</a></li>' +
                    '<li>IP: <span style="color: #a9a9a9;">'+row.ip+'</span></li>' +
                    '<li>地址: <span style="color: #a9a9a9;">'+row.address+'</span></li>' +
                    // '<li>邮箱: <span style="color: #a9a9a9;">'+filterXSS(row.email)+'</span></li>' +
                    '<li>设备: <span style="color: #a9a9a9;">'+row.os + ' ' + row.browser +'</span></li>' +
                    '<li style="color: #a9a9a9;">'+row.createTimeStr+'</li></ul>';
            }
        }, {
            field: 'content',
            title: '内容',
            editable: false,
            width: '260px',
            formatter: function (code, row, index) {
                var content = filterXSS(row.content);
                var source = '<a href="' + appConfig.siteUrl + row.sourceUrl + '" target="_blank">' + row.articleTitle + '</a>';
                var $parent = row.parent;
                var parent = $parent ? '<div style="background-color: #f1f1f1;padding: 5px;margin: 5px;border-radius: 4px;"><div style="padding-left: 10px;"><i class="fa fa-quote-left fa-fw"></i><strong>原评：</strong>' + filterXSS($parent.content) + '</div></div>' : '';
                return '<div style="border-bottom: 1px solid #dcddde;margin-bottom: 10px;">评论自：'+source+'</div>' +
                    '<div class="col-md-12">' + content + parent + '</div>';
            }
        }, {
            field: 'support',
            title: '赞/踩',
            width: '70px',
            editable: false,
            formatter: function (code, row, index) {
                return row.support + "/" + row.oppose;
            }
        }, {
            field: 'status',
            title: '状态',
            width: '70px',
            editable: false,
            formatter: function (code, row, index) {
                var html = '';
                if(code == 'VERIFYING'){
                    html = '<span class="label label-danger">' + row.statusDesc + '</span>';
                } else if (code == 'REJECT') {
                    html = '<span class="label label-warning">' + row.statusDesc + '</span>';
                } else if (code == 'DELETED') {
                    html = '<span class="label label-danger">' + row.statusDesc + '</span>';
                } else {
                    html = '<span class="label label-success">' + row.statusDesc + '</span>';
                }
                return html;
            }
        }, {
            field: 'operate',
            title: '操作',
            width: '200px',
            formatter: operateFormatter //自定义方法，添加操作按钮
        }
    ];
};

/**
 * 检查是否选中
 */
Comment.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Comment.seItem = selected;
        var attr = [];
        for(var i=0;i<selected.length;i++){
            attr.push(selected[i].id);
        }
        Comment.seItemId = attr;
        return true;
    }
};

/**
 * 取消选中编辑或者删除按钮那一行
 */
Comment.cancelClick = function (id) {
    var checkBox = $("a[data-id='"+id+"']").closest("tr").find("td").eq(0).find("input");
    if(checkBox.is(':checked')){
        checkBox.prop('checked', false);
    }else{
        checkBox.prop('checked', true);
    }
};


/**
 * 点击添加文章评论
 */
Comment.openReply = function (id) {
    Comment.cancelClick(id);

    var index = layer.open({
        type: 2,
        title: '文章回复',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/article/comment/comment_reply/'+id
    });
    this.layerIndex = index;
};


/**
 * 点击添加文章评论
 */
Comment.openCheck = function (id) {
    Comment.cancelClick(id);

    var index = layer.open({
        type: 2,
        title: '文章审核',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/article/comment/comment_check/'+id
    });
    this.layerIndex = index;
};

/**
 * 点击添加文章评论
 */
Comment.openAddComment = function () {
    
    var index = layer.open({
        type: 2,
        title: '添加文章评论',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/article/comment/comment_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看文章评论详情
 */
Comment.openCommentDetail = function (id) {
    Comment.cancelClick(id);
    var index = layer.open({
        type: 2,
        title: '文章评论详情',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/article/comment/comment_update/' + id
    });
    this.layerIndex = index;
};

/**
 * 删除文章评论
 */
Comment.delete = function (id) {
     Comment.cancelClick(id);
     var operation = function(){
         var ids = new Array();
         var ajax = new $ax(Feng.ctxPath + "/article/comment/delete", function (data) {
             Feng.pass(data,function () {
                 Feng.success("删除成功!");
                 Comment.table.refresh();
             });
         }, function (data) {
             Feng.error("删除失败!" + data.msg + "!");
         });
         ids.push(id);
         ajax.set("ids", ids);
         ajax.start();
     }
      Feng.confirm("确认删除?", operation);
};


/**
 * 删除文章评论
 */
Comment.deleteMore = function () {
    if (this.check()) {
        var operation = function(){
            var ids = Comment.seItemId;
            var ajax = new $ax(Feng.ctxPath + "/article/comment/delete", function (data) {
                Feng.pass(data,function () {
                    Feng.success("删除成功!");
                    Comment.table.refresh();
                });
            }, function (data) {
                Feng.error("删除失败!" + data.msg + "!");
            });
            ids.push(id);
            ajax.set("ids", ids);
            ajax.start();
        }
        Feng.confirm("确认删除?", operation);
    }

};

/**
 * 查询文章评论列表
 */
Comment.search = function () {
    var queryData = {};
    queryData['condition'] = $("#condition").val();
    Comment.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = Comment.initColumn();
    var table = new BSTable(Comment.id, "/article/comment/list", defaultColunms);
    // table.setPaginationType("client");
    Comment.table = table.init();
});
